from newspaper import Article
import sys

from ui.reader import read
from src.saver import Saver


class Alice:

    def __init__(self):
        self.saver = Saver()

    def fix_url(self, url: str):
        url = url.replace("https://", "")
        url = url.replace("http://", "")
        url = url.replace("www.", "")
        url = url.replace("/", "|")
        return url

    def get_article(self, url: str):
        old_url = url
        url = self.fix_url(url)
        article = self.saver.load_article(url)
        
        if article is None:
            article = Article(old_url)
            article.download()
            article.parse()

            self.saver.save_article(article, url)

        return article
    
    def read_article(self, url: str=""):
        article = self.get_article(url=url)

        read(title=article.title, text=article.text)
    
    def print_articles(self):
        for article in self.saver.articles:
            print(article.title)

if __name__ == "__main__":
    alice = Alice()

    if len(sys.argv) <= 1: 
        alice.print_articles(); exit(0)
    url = sys.argv[1]
    
    alice.read_article(url=url)




